﻿using AntdUI;

namespace DataAcquisition
{
    public partial class ManageProjectForm : BaseForm
    {
        #region 属性
        private Project obj = new Project();
        #endregion

        #region 自定义方法
        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Project> list = SqlHelper.Inquireable<Project>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Project> list)
        {
            var antItemList = new List<ProjectItem>();
            foreach (Project item in list)
            {
                antItemList.Add(new ProjectItem(item.ID,item.Number,item.Name,item.StakeMark,item.PipeSpecification,item.PipeLength,item.Medium,item.DesignPressure,item.PipeNature,item.TestType,item.Company,item.ManometerPrecision,item.ManometerRange,item.TemplateID));
            }
            Table1.DataSource = antItemList;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(string number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Project> list = SqlHelper.Inquireable<Project>(where);
            if (list.Count > 0)
            {
                obj = list[0];
                return true;
            }
            else
            {
                string msg = string.Format("【{0}】工程不存在", number);
                Error(msg);
                obj = new Project();
                return false;
            }
        }
        #endregion

        #region 系统方法
        public ManageProjectForm()
        {
            InitializeComponent();
            FullScreen = true;
            Header.Text = "管理试压工程";
        }

        private void ManageProjectForm_Load(object sender, EventArgs e)
        {
            
            Table1.Height = this.Height;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","工程编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","工程名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("StakeMark","工程桩号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeSpecification","管材规格",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeLength","管道长度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipeNature","管道性质",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Testtype","试压类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Company","施工单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerPrecision","压力表精度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ManometerRange","压力表量程",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center)
            };
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }

        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is ProjectItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckRowExist(data.Number);
                    if (!res)
                    {
                        return;
                    }

                    string msg = string.Format("您确定要删除【{0}】工程吗？", data.Number);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<Project> list = new List<Project>();
                                list.Add(obj);
                                int deleteRes = SqlHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除工程失败");
                                }
                            }
                            else
                            {
                                Error("删除工程失败");
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
